import ajax from '../../utils/request'
Page({
    data: {
        cinemaList: [],
        active: '北京',
        currPage: 1,
        latitude: '',
        longitude: ''
    },
    onShow: function () {
        this.getList({
            cityName: '北京',
            currPage: 1,
            pageSize: 10,
        });
        this.getLocation();
    },
    onChange(e) {
        this.setData({
            currPage: 1
        });
        let data = {
            cityName: e.detail.name,
            currPage: 1,
            pageSize: 10,
        }
        this.getList(data)
    },
    async getList(data) {
        try {
            let res = await ajax('http://43.136.85.99:3008/cinema/list', data);
            res = res.map(item => {
                item.lowPrice = (item.lowPrice / 100).toFixed(2);
                if (this.data.longitude) {
                    let distance = this.getDistance(item.latitude, item.longitude, this.data.latitude, this.data.longitude);
                    if (distance < 1) {
                        distance = (distance * 1000).toFixed(0) + 'm'
                        item.distance = distance
                    } else {
                        distance = distance.toFixed(2) + 'km'
                        item.distance = distance
                    }
                }
                return item;
            });
            if (data.currPage === 1) {
                this.setData({
                    cinemaList: res
                });
            } else {
                this.setData({
                    cinemaList: this.data.cinemaList.concat(res)
                });
            }
        } catch (error) {
            console.error('请求数据时出现错误:', error);
        }
    },
    async getLocation() {
        wx.getLocation({
            type: 'wgs84',
            success: (res) => {
                const latitude = res.latitude;
                const longitude = res.longitude;
                wx.request({
                    url: 'https://apis.map.qq.com/ws/geocoder/v1/',
                    data: {
                        location: `${latitude},${longitude}`,
                        key: 'XBZBZ-OBG63-LOD3N-3QR5Q-X6Z2Q-BFBIR',
                        get_poi: 0
                    },
                    success: function (res) {
                        if (res.data.status === 0) {
                            let city = res.data.result.address_component.city
                            city = city.slice(0, -1)
                            this.setData({
                                active: city,
                                latitude: latitude,
                                longitude: longitude
                            });
                        }
                    }.bind(this)
                });
            },
            fail: (err) => {
                console.error('获取位置失败', err);
            }
        });
    },
    // 计算两地距离
    getDistance(lat1, lng1, lat2, lng2) {
        var radLat1 = lat1 * Math.PI / 180.0;
        var radLat2 = lat2 * Math.PI / 180.0;
        var a = radLat1 - radLat2;
        var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
        var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
            Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * 6378.137; // EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return s;
    },
    onReachBottom() {
        // 页面滑动到底部时，将 currPage 加 1
        let newCurrPage = this.data.currPage + 1;
        this.setData({
            currPage: newCurrPage
        });
        let data = {
            cityName: this.data.active,
            currPage: newCurrPage,
            pageSize: 10,
        };
        this.getList(data);
    }
})